package org.bringer.webapp.pages;

import java.lang.annotation.Annotation;
import java.util.List;

import org.apache.tapestry5.PropertyConduit;
import org.apache.tapestry5.alerts.AlertManager;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.beaneditor.PropertyModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;
import org.bringer.dao.SearchException;
import org.bringer.model.Account;
import org.bringer.service.AccountManager;
import org.bringer.webapp.services.SecurityContext;
import org.slf4j.Logger;


/**
 * Main entry point of the application
 *
 * @author Serge Eby
 * @version $Id: Home.java 5 2008-08-30 09:59:21Z serge.eby $
 */
public class Accounts {
	@Property
    private String errorMessage;
    @Inject
    private Logger logger;

    @Inject
    private Messages messages;

    @Inject
    private SecurityContext securityContext;
    
    @Inject
    private BeanModelSource beanModelSource;

    @Inject
    private AlertManager alertManager;

    @InjectPage
    private AccountEdit accountEdit;
    
    @Inject
    private AccountManager accountManager;

    @Persist
    @Property
    private Account currentAccount;
    @Property
    private List<Account> accounts;
    private Double currentBalance;
    public BeanModel<Account> getModel() {
        final BeanModel<Account> model = beanModelSource.createDisplayModel(Account.class, messages);

        model.include("ibanCode", "description", "balance");
        // Set labels
        model.get("ibanCode").label(messages.get("account.ibanCode"));
        model.get("description").label(messages.get("account.description"));
        model.get("balance").label(messages.get("account.balance"));
      
        final PropertyModel pm1 = model.add("current", 
        		createCurrentBalancePropertyConduit()).label(messages.get("account.current"));
        pm1.sortable(true);
	        
	     

        return model;
    }
    private PropertyConduit createCurrentBalancePropertyConduit() {
		// TODO Auto-generated method stub
		return new PropertyConduit() {
			
			@Override
			public <T extends Annotation> T getAnnotation(Class<T> arg0) {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public void set(Object instance, Object value) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public Class getPropertyType() {
				// TODO Auto-generated method stub
				return Double.class;
			}
			
			@Override
			public Object get(Object instance) {
				Account ac=(Account)instance;
				Double res=0.0;
				if (ac.getCurrentBalance()!=null){
					res=ac.getCurrentBalance().getCurrentBalance();
				}
				
				return res;
			}
		};
	}
	void setupRender() {
        try {
        	accounts = accountManager.getAccountsByUser("-2");
        } catch (SearchException se) {
            errorMessage = se.getMessage();           
        }
    }
    
	 Object onAdd() {
	        return accountEdit.initialize(null, "list");
	 }
    public Double getCurrentBalance() {
    	Double result=currentAccount.getBalance();
    	if (currentAccount.getCurrentBalance()!=null){
    		result=currentAccount.getCurrentBalance().getCurrentBalance();
    	}
		return result;
	}
	
    Object onActionFromEdit(Account currentAccount) {
        logger.debug("fetching account with iban: " + currentAccount);
        
        return accountEdit.initialize(currentAccount, "list");
    }
    
}
